package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.*;
import com.javasm.service.MisMannageService.ShowService;
import com.javasm.service.MisMannageService.impl.ShowServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/recommend/*")
public class RecommendServlet extends BaseServlet {


    protected void getrec(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String idStr = req.getParameter("id");


        Integer id = 1;


        if (idStr != null & !"".equals(idStr)) {
            id = Integer.parseInt(idStr);
        }

        ShowService showService = new ShowServiceImpl();
        List<TbShow> tbShows = showService.getrec(id);
        //System.out.println(tbShows);

        ReturnEntity returnEntity = new ReturnEntity();
        if (tbShows != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(tbShows);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void assoSubscriptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String idStr = req.getParameter("id");
        //System.out.println(ids);
        Integer id = null;
        if (idStr != null & !"".equals(idStr)) {
            id = Integer.parseInt(idStr);
        }
        ShowService showService = new ShowServiceImpl();
        Integer index = showService.editSubs(ids, id);
        //System.out.println(index);

        ReturnEntity returnEntity = new ReturnEntity();
        if (index > 0) {
            returnEntity.setRetCode(ReturnCode.EDIT_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_OK.getMsg());
        } else {
            returnEntity.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void relevancy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //String showProdName = req.getParameter("showProdName");

        ShowService showService = new ShowServiceImpl();
        List<TbShow> allShow = showService.getAllShow();
       // System.out.println(allShow);

        ReturnEntity returnEntity = new ReturnEntity();
        if (allShow != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(allShow);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
       // System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String prodIdStr = req.getParameter("showProdId");
        String showRecommend = req.getParameter("showRecommend");
        String visible = req.getParameter("visible");
        String online = req.getParameter("subscribeUpwards");
        String starting = req.getParameter("starting");
        String sortStr = req.getParameter("sort");
        String reason = req.getParameter("reason");

        //System.out.println(prodIdStr+showRecommend+visible+starting+online+sortStr+reason);
        Integer prodId = null;
        if (prodIdStr != null & !"".equals(prodIdStr)) {
            prodId = Integer.parseInt(prodIdStr);
        }


        Integer sort = null;
        if (sortStr != null & !"".equals(sortStr)) {
            sort = Integer.parseInt(sortStr);
        }

        boolean visible1 = true;
        if (visible != null & !"".equals(visible)) {
            visible1 = Boolean.parseBoolean(visible);
        }

        boolean starting1 = true;
        if (starting != null & !"".equals(starting)) {
            starting1 = Boolean.parseBoolean(starting);
        }

        boolean online1 = true;
        if (online != null & !"".equals(online)) {
            online1 = Boolean.parseBoolean(online);
        }

        TbShow tbShow = new TbShow();
        tbShow.setSort(sort);
        tbShow.setShowProdId(prodId);
        tbShow.setShowRecommend(showRecommend);
        tbShow.setVisible(visible1);
        tbShow.setSubscribeUpwards(online1);
        tbShow.setStarting(starting1);
        tbShow.setReasons(reason);
        ShowService showService = new ShowServiceImpl();
        Integer editshow = showService.editshow(tbShow);


        ReturnEntity returnEntity = new ReturnEntity();
        if (editshow > 0) {
            returnEntity.setRetCode(ReturnCode.EDIT_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_OK.getMsg());
        } else {
            returnEntity.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String prodIdStr = req.getParameter("showProdName");
        String showRecommend = req.getParameter("showRecommend");
        String visible = req.getParameter("visible");
        String online = req.getParameter("online");
        String starting = req.getParameter("starting");
        String sortStr = req.getParameter("sort");
        String reason = req.getParameter("reason");

        //System.out.println(prodIdStr+showRecommend+visible+starting+online+sortStr+reason);
        Integer prodId = null;
        if (prodIdStr != null & !"".equals(prodIdStr)) {
            prodId = Integer.parseInt(prodIdStr);
        }

        Integer sort = null;
        if (sortStr != null & !"".equals(sortStr)) {
            sort = Integer.parseInt(sortStr);
        }

        boolean visible1 = true;
        if (visible != null & !"".equals(visible)) {
            visible1 = Boolean.parseBoolean(visible);
        }

        boolean starting1 = true;
        if (starting != null & !"".equals(starting)) {
            starting1 = Boolean.parseBoolean(starting);
        }

        boolean online1 = true;
        if (online != null & !"".equals(online)) {
            online1 = Boolean.parseBoolean(online);
        }

        TbShow tbShow = new TbShow();
        tbShow.setSort(sort);
        tbShow.setShowProdId(prodId);
        tbShow.setShowRecommend(showRecommend);
        tbShow.setVisible(visible1);
        tbShow.setSubscribeUpwards(online1);
        tbShow.setStarting(starting1);
        tbShow.setReasons(reason);
        ShowService showService = new ShowServiceImpl();
        Integer addshow = showService.addshow(tbShow);
        //System.out.println(addshow+1232132123);
        ReturnEntity returnEntity = new ReturnEntity();
        if (addshow > 0) {
            returnEntity.setRetCode(ReturnCode.ADD_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.ADD_OK.getMsg());
        } else {
            returnEntity.setRetCode(ReturnCode.ADD_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.ADD_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void getProdName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ShowService showService = new ShowServiceImpl();
        List<ProdInfo> prodName = showService.getProdName();
    //    System.out.println(prodName);

        ReturnEntity returnEntity = new ReturnEntity();
        if (prodName != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(prodName);
         //   System.out.println(prodName);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
      //  System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }

    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        String prodName = req.getParameter("seriesname");
        //System.out.println(prodName + "aaasssasasassa");

        Integer page = 1;
        Integer pageSize = 10;
        if (pageStr != null & !"".equals(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (pageSizeStr != null & !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        ShowService showService = new ShowServiceImpl();
        List<TbShow> showById = showService.getShow(page, pageSize, prodName);
        Integer totalNum = showService.getTotalnum(prodName);
        //System.out.println(totalNum);
        PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);

        ReturnEntity returnEntity = new ReturnEntity();
        if (showById != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(showById);
            returnEntity.setPageInfo(pageInfo);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        //System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();
    }
}
